package com.gis507.test.AISDataSort;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.junit.Test;

import java.io.IOException;
import java.util.*;
import java.util.stream.Stream;

/**
 * 1、继承Reducer
 * 2、设置输入输出类型
 * 3、重写reduce方法
 */
@Slf4j
public class AISDataReducer extends Reducer<Text, AISDataBean, Text, AISDataBean> {

    @Override
    protected void reduce(Text key, Iterable<AISDataBean> values, Context context) throws IOException, InterruptedException {

        //实例化一个列表，用于存放数据
        List<AISDataBean> aisList = new ArrayList<>(1000);

        //遍历values
        for (AISDataBean value : values) {
            //将value添加到列表
            aisList.add(new AISDataBean(value.getSpeed(),value.getUnixTime(),value.getLon_d(),value.getLat_d(),
                    value.getDraught(),value.getLength(),value.getWidth(),value.getCallsign(),value.getCourse(),
                    value.getRot()));
//            aisList.add(value);
            log.info(key + "," + value.getUnixTime());

        }

        //使用lambda表达式对aisList按照时间进行排序
//        aisList.stream().sorted(Comparator.comparingLong(AISDataBean::getUnixTime));

        //使用lambda表达式排序
        aisList.sort(AISDataBean::compareTo);

        for (AISDataBean aisDataBean : aisList) {


            context.write(key,aisDataBean);
        }
    }

    @Test
    public void test(){

        List<Object> list1 = Arrays.asList(123, 345, new Date(),new AISDataBean(),new AISDataBean(1,2,3,4,"ni","ji","ni","m",1,1));

        ArrayList<Object> list2 = new ArrayList<>();

        for (Object o : list1) {
            System.out.print(o + "============");
            list2.add(o);
//            for (Object o1 : list2) {
//                System.out.print(o1 + ",");
//            }
//            System.out.println();
        }
        System.out.println("====================================");
        for (Object o : list2) {

//            log.info(o.toString());
            System.out.println(o);
        }


    }
}
